<template>
	<view class="content">
		<uni-search-bar  v-model="searchValue"  @confirm="Search"
						  cancelButton="none" placeholder="请输入船舶名称">
		</uni-search-bar>
		<uni-card v-for="ship in ships" :title="ship.ship_name" :extra="ship.ship.voyage" @click="toShipInfo(ship._id)" v-if="update">
			{{ship.createAt}}
		</uni-card>
		<!-- <view class="text-area">
			<text class="title">{{title}}</text>
		</view> -->
		<uni-fab ref="fab" horizontal="right"
				 @fabClick="addShip" />
	</view>
</template>

<script>
	import common from  "../common/js/common.js";
	export default {
		data() {
			return {
				searchValue: '',
				ships:[],
				title: 'Hello',
				searchValue: '',
				update: true, 
			}
		},
		onLoad() {
			uni.request({
					url: common.baseurl + '/api/ship/all',
					method:'GET',
				}).then(data=>{
					if(undefined != data[1].data.errCode){
						uni.showToast({
						          title: data[1].data.errMsg,
						          icon: 'none',
						        });
					}else{
						console.info(data[1].data)
						this.ships = data[1].data
					}
			
				}).catch(()=>{
			  
				})
			// uni.login({
			//     provider: 'weixin',
			//     success: (res) => {
			// 		alert(res.code);
			// 		const request = require('request');
			// 		const appid = '492b0835afc17945882fa05774ada7b6';
			// 		const secret = 'wx38e96d1dee767901';
					
			// 		const url = `https://api.weixin.qq.com/sns/jscode2session?appid=${appid}&secret=${secret}&js_code=${res.code}&grant_type=authorization_code`;
					
			// 		request.get(url, (error, response, body) => {
			// 		    if (!error && response.statusCode === 200) {
			// 		        const data = JSON.parse(body);
			// 		        // data.openid 和 data.session_key 即为获取到的 openid 和 session_key
			// 		    }
			// 		});
			//         // res.code 即为获取到的 code
			//     }
			// });
			

			
			
			// uni.request({
			// 		url: '/api/xiangmu/all',
			// 		method:'GET',
			// 		// data: this.login
			// 	}).then(data=>{
			// 		console.info(data[1].data)
			// 		this.ships = data[1].data
			// 	}).catch(()=>{

			// 	})
		},
		methods: {
			Search(){
				var Url = common.baseurl + '/api/ship/all'
				if(this.searchValue != ''){
					Url = Url + '?ship_name=' + this.searchValue
				}
				uni.request({
						url:Url,
						method:'GET' ,
					}).then(data=>{
						if(undefined != data[1].data.errCode){
							uni.showToast({
							          title: data[1].data.errMsg,
							          icon: 'none',
							        });
						}else{
							console.info(data[1].data)
							this.ships = data[1].data
						}
				
					}).catch(()=>{
				  
					})
			},
			addShip(){
				uni.navigateTo({
					url: 'add_ship'
				});
			},
			toShipInfo(ship_id){
				uni.navigateTo({
					url: "ship_info?ship_id=" + ship_id
				});
			},
		}
	}
</script>

<style>
	.content {
		/* display: flex;
		flex-direction: column;
		align-items: center;
		justify-content: center; */
		
	}

	.logo {
		height: 200rpx;
		width: 200rpx;
		margin-top: 200rpx;
		margin-left: auto;
		margin-right: auto;
		margin-bottom: 50rpx;
	}

	.text-area {
		display: flex;
		justify-content: center;
	}

	.title {
		font-size: 36rpx;
		color: #8f8f94;
	}
</style>
